package com.xm.servlet.reception_desk.account;

import com.xm.service.reception_desk.AccountService;
import com.xm.util.SmsUtil;
import lombok.SneakyThrows;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Random;

/**
 * @author 田顺琪
 * @date 2024-01-16
 */
@WebServlet(name = "AccountServlet", value = "/account/servlet")
public class AccountServlet extends HttpServlet {

    private final SmsUtil smsUtil=new SmsUtil();

    /**
     * 用户service
     */
    private final AccountService accountService=new AccountService();
    /** 发送手机验证码
     * @param req   an {@link HttpServletRequest} object that
     *                  contains the request the client has made
     *                  of the servlet
     *
     * @param resp  an {@link HttpServletResponse} object that
     *                  contains the response the servlet sends
     *                  to the client
     *
     */
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phone=req.getParameter("phone");
        String password=req.getParameter("password");
        //AES解密
        String informationId = req.getParameter("informationId");

        boolean ret=accountService.account(phone,password,Integer.parseInt(informationId));
        if (ret){
            Random random = new Random();
            Integer integer = SmsUtil.setSms(phone, random.nextInt(9000) + 1000);
            System.out.println(integer);
            resp.getWriter().print(integer);
        }
    }



    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String informationId = req.getParameter("informationId");
        String action= req.getParameter("action");
        String order="order";
        String delete="delete";
        if (action.equals(order)){
            int count=accountService.count(Integer.parseInt(informationId));
            resp.getWriter().print(count);
        } else if (action.equals(delete)) {
            boolean ret = accountService.accountDelete(Integer.parseInt(informationId));
            resp.getWriter().print(ret);
        }
    }
}
